package workforce.limousine.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import limousine.vo.Users;
import limousine.vo.RoleSystems;
import limousine.dbservice.RoleSystemsService;
import limousine.util.Service;
import limousine.util.Token;
import limousine.util.Func;

public class RoleSystemsManagerInsert extends Action {

    private String errorMsg = "Process Fail";
    private String id = "";
    private String mode = "";
    private Func tool = new Func();

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        HttpSession session = request.getSession();
        System.out.println("***RoleSystemsManagerInsert");

        Users user = (Users) session.getAttribute(Token.USER_SESSION);
        if (user == null) {
            System.out.println("--SESSION EXPIRE--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }
       /* if (!user.getRole_name().equals("Admin")) {
            System.out.println("--Not Admin Hack--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }*/

        //        DataSource ds = this.getDataSource(request, Token.DATASOURCE_NAME);
        Service service = new Service();
        service.setServletContext(this.getServlet().getServletContext());
        DataSource ds = service.getDataSource();
        service.removeMsgAttribute(request, session, "");

        DynaActionForm daf = (DynaActionForm) form;
        mode = request.getParameter("mode");

        if (mode.equals("1")) {//edit mode
            id = request.getParameter("eid");
        }

        boolean success = saveData(daf, ds);
        if (success) {
            session.setAttribute(Token.MSG_USER,Token.MSG_INSERT_SUCCESS);
            return mapping.findForward(Token.SUCCESS);
        } else {
            session.setAttribute(Token.MSG_ERROR, errorMsg);
            return mapping.findForward(Token.FAIL);
        }
    }

    private String checkEmptyString(String value) {// optimize data
        value = (value == null) ? "" : value.trim();
        return tool.jsp2UTF(value);
    }

    public boolean saveData(DynaActionForm d, DataSource ds) {

        String roleId = d.getString("roleId");
        roleId = checkEmptyString(roleId);
        if (roleId.length() == 0) {
            errorMsg = "Not have roleId";
            return false;
        }

        String[] systemsId = d.getStrings("systemsId");
        System.out.print("Xxxxxxxxxxx " + systemsId.length);
        if (systemsId == null) {
            errorMsg = "Not have systemsId";
            return false;
        }

        RoleSystems o = new RoleSystems();
        o.setId(id);
        o.setRoleId(roleId);

        RoleSystemsService rss = new RoleSystemsService();
        rss.setDataSource(ds);

        if (mode.equals("1")) {//edit
            rss.deleteByRoleId(roleId);//delete old
        }
        for (int i = 0; i < systemsId.length; i++) {
            o.setSystemsId(systemsId[i]);
            rss.insert(o);
        }

        return true;
    }
}
